home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 3565-4.665 / dmg-4385 / issue_10 / 16.pne < prev    next >
Text File  |  1987-04-21  |  8KB  |  187 lines

  1.  
  2.  
  3.                              DISK   ACCESSING
  4.                              ~~~~~~~~~~~~~~~~
  5.                             By  Tony Greenwood
  6.                           (For STOSSER Diskzine)
  7.                           ~~~~~~~~~~~~~~~~~~~~~~
  8.  
  9. Disk (or is it disc) access is a very important and powerful part of 
  10. programming.  Don't you just hate playing a game on your ST for hours and 
  11. finally making the highscore table only for it not to be saved to disk and 
  12. therefore losing it forever.  I would much prefer to know that the next 
  13. person who is going to play will be able to marvel at my high score.  Not 
  14. just high score tables can be saved and retrieved from your disk, there 
  15. are numerous uses and advantages that can be gained from disk accessing. 
  16. The file you are now reading has been accessed from disk of course. 
  17. Theres also a save to disk option in the help screen, I am sure you can 
  18. think of many more uses.  But do you know how to do it, I think not or you 
  19. would'nt be reading this file.  Well i have the problem of not knowing how 
  20. much or how little you do know, therefore i will have to start from 
  21. scratch and presume you know nothing on the subject.  A little boring for 
  22. some of you i know, but at least i know i will have covered all levels of 
  23. competence.
  24.  
  25. Well i have the STOS Manual at my side to refer to.  Its now some time 
  26. since i learnt how to use these commands and its only now that i realise 
  27. just how useless the manual is.  The commands i first learnt and the ones 
  28. we will be covering ie: SEQUENTIAL FILES, "FILENAME.SEQ"
  29.  
  30. Before i go any further i will stop you falling into the same trap i did 
  31. when i was trying to learn these, and that is the fact that the files we 
  32. want to access do not have to have SEQ as there extensions.  So we can use 
  33. all the following on DOC, TXT, MUS, ASC etc etc... 
  34.  
  35. Right then, the word sequence means that any access to these files will be 
  36. done in order from start of file to end of file. In other words if you 
  37. wanted to load line 300 from a DOC file you would have to load the first 
  38. 299 lines to get to it.  Random access files would let you load line 300 
  39. on its own, or save line 300 on its own.  In order for us to save one line 
  40. we would have to save the whole file, apart from the making of a database 
  41. program i can think of no reason to want to load just the one part of a 
  42. file.  In fact i have never needed that system so we will just concentrate 
  43. on SEQUENCIAL FILES.
  44.  
  45.                       ~~~~~~~ LOADING ~~~~~~~
  46.  
  47. First off before we open a file and load it into memory we will need 
  48. somewhere to put it.  Lets concentrate on loading a whole doc file into 
  49. memory shall we?.  So we would load this doc file line by line, the doc 
  50. file could be any legnth could'nt it, so we will DIM an array of 1000. 
  51. This will then enable us to load any doc file that is 1000 lines long or 
  52. less.  If you think you may need bigger then simply DIM a bigger array of 
  53. course, so we are off!.
  54.  
  55.  10 DIM TXT$(1000)
  56.  
  57. Next we need to find a file to open, so we will use the file selector to 
  58. find one.  You will find the instructions for this in page 218 of your 
  59. manual.  But i have just looked at it and its not very clear is it, here 
  60. i will show you what to do.
  61.  
  62.  20 F$=FILE SELECT$ ("*.*")
  63.  
  64. This will bring up a file selector that will list all the files and 
  65. folders in the root directory.  The "*" simply mean show all, so it will 
  66. show all the file names and all the extensions.  If you only wanted DOC 
  67. files to be listed then you would change it to:
  68.  
  69.  20 F$=FILE SELECT$ ("*.DOC") 
  70.  
  71. Then whatever file you click on, F$ will hold the name of that file, so if 
  72. you click on READ_ME .DOC , then thats what F$ would equal.
  73.  
  74. Next we want to open that file, we can have more than one file open at 
  75. once, so we have to keep them numbered.  We then use that number for 
  76. everything we do to that file so as not to confuse STOS.  If you are only 
  77. using one file at once then you must still number it.  So to keep things 
  78. as clear as possible we will call this one number one, but it must be 
  79. prefixed by a #,
  80. So we are working on file #1
  81.  
  82.  30 OPEN IN #1, F$
  83.  
  84. "Open" speaks for itself ( i hope ) ,"F$" holds the filename,"#1"  is the 
  85. number of the file we are using, "IN" means we are opening the file for 
  86. retrieving/getting data and bringing it into our program.  The oposite 
  87. function ie: saving data is the opposite command ie: open out #1, f$, but 
  88. dont worry about that yet, so long as you fully understand line 30 and 
  89. what each peice means.
  90.  
  91. Now we obviously want more than one peice of information bringing in, ie: 
  92. each line of the DOC file is a seperate peice of info that we will be 
  93. puttting into each cell of our array.  Therefore we will now need to set 
  94. up a loop in order to bring it all in.
  95.  
  96.  40 E=0 : REPEAT 
  97.  50 LINE INPUT #1,TXT$(E)
  98.  60 INC E
  99.  70 UNTIL EOF(#1)
  100.  80 DEC E 
  101.  90 CLOSE #1 : rem we have got it!
  102.  
  103.  Now to explain the above,
  104.  
  105. Line 40 sets E to 0 as that is the pointer to tell the computer where to 
  106. put the info or which part of the array to put it in.  Then REPEAT starts 
  107. the loop.
  108.  
  109. Line 50 gets the first line of the doc we are loading and puts it into 
  110. TXT$(0)
  111.  
  112. Line 60 of course increases E
  113.  
  114. Line 70 is the end of the loop and checks for the last peice of 
  115. information in the document we are loading , or End Of File EOF and the 
  116. number of the file, So EOF(#1) checks for end of file one,
  117.  
  118. If it is the end then we goto line 80, as we have been increasing E after 
  119. we have put our information in it.  This means that currently TXT$(E) has 
  120. nothing in it, so we dec E to go back to the last peice of information 
  121. that we loaded.
  122.  
  123. Line 90 then closes the file we have been using, ie: #1, In all your 
  124. routines.  If you have an OPEN Then you must have a close,
  125.  
  126.  
  127.                     ~~~~~~~~~ SAVING ~~~~~~~~~
  128.  
  129. Well this bit should be a lot shorter than the above as it is quite simple 
  130. now we have covered the main points of disk accessing, we are quite simply 
  131. going to do the opposite of the loading instructions, let me explain..
  132.  
  133. To open a file for Saving something to disk we use the following command.
  134.  
  135.  OPEN OUT #1, "FILENAME.DOC"
  136.  
  137. Or if you have just loaded a doc file or an high score table or something 
  138. with a variable as in the loading commands, open in #1, F$, then you can 
  139. use the same variable
  140.  
  141. OPEN OUT #1,F$
  142.  
  143. Then instead of INPUT #1, TXT$(E) we would do the opposite and..
  144.  
  145. PRINT #1, TXT$(E)
  146.  
  147. So to save out the doc file you loaded in with the loading commands, you 
  148. would now use the following.
  149.  
  150.  100 OPEN OUT #1,F$
  151.  110 FOR N=0 TO E
  152.  120 PRINT #1,TXT$(N)
  153.  130 NEXT N
  154.  140 CLOSE #1
  155.  
  156. The EOF(#1) will not work when saving out, but we already know how many 
  157. lines to save as we got that with the E variable, so its easy enough to 
  158. make a controlled loop from 0 to E and we cannot miss then can we. 
  159.  
  160.  Please note : When you open out a filename, it will erase any file with 
  161. that filename thats on the disk.  Therefore if you had a doc file with 
  162. lets say five hundred lines in it and you wanted to add one line, you 
  163. cannot open the file and print to disk just line TXT$(501) ,because then 
  164. the file with that filename will then simply consist of the one line you 
  165. just saved.  Hence the explanation earlier about SEQUENCE, you would have 
  166. to load the whole five hundred lines, then add one line, then save the 
  167. five hundred and one lines.
  168.  
  169. The use of RANDOM ACCESS files will however let you load or save just one 
  170. line, but i dont know enough on random access files to enable me to write an article on the subject, If you do then 
  171. write it up for us!.
  172.  
  173. Well i hope the above is enough to get you started on using the saving and 
  174. loading commands that are at your disposal.  There are lots of other 
  175. commands that can be used in conjunction with the above.  I suggest that 
  176. once you can do the above then the rest will come to you a lot easier with 
  177. the use of your trusty manual.
  178.  
  179.  HAPPY STOSSING!
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.